
package com.scs.application.modules.upms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.model.PageResult;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.mvc.controller.ImAndExAbleController;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.upms.entity.UserBlock;
import com.scs.application.modules.upms.request.UserBlockQueryRequest;
import com.scs.application.modules.upms.security.UserPrincipal;
import com.scs.application.modules.upms.service.UserBlockService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户区块 controller
 *
 */
@AllArgsConstructor
@RestController
@RequestMapping("/upms/user_blocks")
public class UserBlockController extends CrudController<UserBlock, String, UserBlockService> implements ImAndExAbleController<UserBlockQueryRequest> {


    @GetMapping("/page")
    public PageResult page(UserBlockQueryRequest request) {
        IPage page = service.page(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/list")
    public CommonResult list(UserBlockQueryRequest request) {
        List<UserBlock> list = service.list(request);
        return CommonResult.ok("查询成功", list);
    }


    /**
     * 获取当前用户的区块
     *
     * @return
     */
    @GetMapping("/current_user_blocks")
    public CommonResult listCurrentUserBlock() {
        List<UserBlock> list = service.listCurrentUserBlock();
        return CommonResult.ok("查询成功", list);
    }

    @PostMapping("/restore_blocks")
    public CommonResult restoreBlocks() {
        UserPrincipal user = UserUtils.currentUser();
        service.restoreBlocks(user.getLoginKey());
        return CommonResult.ok("初始化成功");
    }

}
